package pip.can;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Controller extends Thread{
	private HashMap<String, Node> topology; //key - unikalna nazwa węzła - taka jak na grafie //value - obiekt węzła
	private ArrayList<String> clients;
	Controller(HashMap<String, Node> topology,ArrayList<String>  clients){	
		this.topology = topology;
		this.clients = clients;
	}


	public void run(){
		System.out.println("Poczatek symulacji");
		
		Repository repo = new Repository();
		StaticVariables.setRepository(repo);
		HashMap<Integer,Integer> trafficClasses = new HashMap<Integer,Integer>(); //<id, class>
		int contentPerClass = StaticVariables.Contents/StaticVariables.Classes;
		int classId = 0;
		for (int i = 0 ; i<StaticVariables.Contents; i++){
			if (i != 0 && i%contentPerClass == 0){
				classId++;
			}
			int contentGeomSize = Distribution.geometricRandNum((double)1.0/(double)StaticVariables.contentSize) *StaticVariables.chunkSize ;
			repo.addContent(new Content(contentGeomSize,i));
			trafficClasses.put(i, classId);
		}
		
		Simulator sim=new Simulator(topology);
		//KLASY RUCHU - hashmap
		double t = 0;
		ZipfGenerator z = new ZipfGenerator(StaticVariables.Classes,1.1);
		double endTime = StaticVariables.endTime;
		int tmp = 0;
		while(t<endTime)
		{
			int zipf = z.next();
			int uniformRandom = (int) (Math.random()*contentPerClass);
			int contId = zipf*contentPerClass+uniformRandom;
			t=t+(Distribution.poissonRandNum(1.0*((double)zipf+1.0)));
				sim.addRequest(clients.get(tmp%clients.size()), contId, zipf, t); //który klient wysyła żądanie?
			//System.out.println("Time = " + t + " TrafficClass = " + zipf + " ContentId = " + contId);
			tmp++;
		}

		sim.start();
		for (int r=0;r<StaticVariables.Classes;r++){
			System.out.println("Klasa " + r + " time = " + StaticVariables.Results[r]);
		}
		StaticVariables.resultBtn.setEnabled(true);

	}
	
}
